/*
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at 
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  
 */

using System;
using java = biz.ritter.javapi;
using org.apache.commons.collections;

namespace org.apache.commons.collections.functors
{

    /**
     * Closure implementation that chains the specified closures together.
     * 
     * @since Commons Collections 3.0
     * @version $Revision$ $Date$
     *
     * @author Stephen Colebourne
     */
    public class ChainedClosure : Closure, java.io.Serializable
    {

        /** Serial version UID */
        private static readonly long serialVersionUID = -3520677225766901240L;

        /** The closures to call in turn */
        private readonly Closure[] iClosures;

        /**
         * Factory method that performs validation and copies the parameter array.
         * 
         * @param closures  the closures to chain, copied, no nulls
         * @return the <code>chained</code> closure
         * @throws IllegalArgumentException if the closures array is null
         * @throws IllegalArgumentException if any closure in the array is null
         */
        public static Closure getInstance(Closure[] closures)
        {
            FunctorUtils.validate(closures);
            if (closures.Length == 0)
            {
                return NOPClosure.INSTANCE;
            }
            closures = FunctorUtils.copy(closures);
            return new ChainedClosure(closures);
        }

        /**
         * Create a new Closure that calls each closure in turn, passing the 
         * result into the next closure. The ordering is that of the iterator()
         * method on the collection.
         * 
         * @param closures  a collection of closures to chain
         * @return the <code>chained</code> closure
         * @throws IllegalArgumentException if the closures collection is null
         * @throws IllegalArgumentException if any closure in the collection is null
         */
        public static Closure getInstance(java.util.Collection<Object> closures)
        {
            if (closures == null)
            {
                throw new java.lang.IllegalArgumentException("Closure collection must not be null");
            }
            if (closures.size() == 0)
            {
                return NOPClosure.INSTANCE;
            }
            // convert to array like this to guarantee iterator() ordering
            Closure[] cmds = new Closure[closures.size()];
            int i = 0;
            for (java.util.Iterator<Object> it = closures.iterator(); it.hasNext(); )
            {
                cmds[i++] = (Closure)it.next();
            }
            FunctorUtils.validate(cmds);
            return new ChainedClosure(cmds);
        }

        /**
         * Factory method that performs validation.
         * 
         * @param closure1  the first closure, not null
         * @param closure2  the second closure, not null
         * @return the <code>chained</code> closure
         * @throws IllegalArgumentException if either closure is null
         */
        public static Closure getInstance(Closure closure1, Closure closure2)
        {
            if (closure1 == null || closure2 == null)
            {
                throw new java.lang.IllegalArgumentException("Closures must not be null");
            }
            Closure[] closures = new Closure[] { closure1, closure2 };
            return new ChainedClosure(closures);
        }

        /**
         * Constructor that performs no validation.
         * Use <code>getInstance</code> if you want that.
         * 
         * @param closures  the closures to chain, not copied, no nulls
         */
        public ChainedClosure(Closure[] closures)
            : base()
        {
            iClosures = closures;
        }

        /**
         * Execute a list of closures.
         * 
         * @param input  the input object passed to each closure
         */
        public void execute(Object input)
        {
            for (int i = 0; i < iClosures.Length; i++)
            {
                iClosures[i].execute(input);
            }
        }

        /**
         * Gets the closures, do not modify the array.
         * @return the closures
         * @since Commons Collections 3.1
         */
        public Closure[] getClosures()
        {
            return iClosures;
        }

    }
}